package com.dp.mallchat.common.user.comsumer;

import com.dp.mallchat.common.common.constant.MQConstant;
import com.dp.mallchat.common.common.domain.dto.PushMessageDTO;
import com.dp.mallchat.common.user.domain.ws.enums.WSPushTypeEnum;
import com.dp.mallchat.common.user.service.WebSocketService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: dupeng
 * @CreateTime: 2024-07-05  18:48
 * @Description: 消息推送用户消费
 */
@Component
@RocketMQMessageListener(consumerGroup = MQConstant.PUSH_GROUP, topic = MQConstant.PUSH_TOPIC, messageModel = MessageModel.BROADCASTING)
@Slf4j
public class PushConsumer implements RocketMQListener<PushMessageDTO> {

    @Autowired
    private WebSocketService webSocketService;

    @Override
    public void onMessage(PushMessageDTO message) {
        //推送在线用户
        switch (WSPushTypeEnum.of(message.getPushType())) {
            case ALL:
                webSocketService.sendToAllOnlineUser(message.getWsBaseMsg(), null);
                break;
            case USER:
                message.getUidList().forEach(uid -> {
                    webSocketService.sendToUid(message.getWsBaseMsg(), uid);
                });
                return;
        }

    }
}
